/**
 * 格式化仪表盘类
 * @des {
 *   仅格式化组件所需要的数据
 * }
 */
import { $each, $isEmptyArr } from '../index';
import { handleConditionExtend } from '../conditionFormat/useConditionFormat.js';
/**
 * 格式化仪表盘series
 * @param {object} datasets
 */
export function processBasicGaugeData(datasets) {
  const { dimensions, source, fieldNameChainMap, conditionData, conditionOptions } = datasets;
  const { yAxis, yAxisCondition } = dimensions;
  const sourceData = [];
  let yAxisConditionArr = [];//用来单独存储条件项名称
  // 条件项存在多度量
  if (yAxisCondition && yAxisCondition.includes('&')) {
    yAxisConditionArr = yAxisCondition.split('&');
  } else {
    yAxisConditionArr = [yAxisCondition];
  }
  $each(source, (item) => {
    item[yAxis] && sourceData.push({
      value: item[yAxis],
      name: yAxis,
    });
  });
  const seriesData = [{
    name: yAxis,
    dataFormatType: 'yAxis',
    data: sourceData,
  }];
  // 根据原始字段名称收集数据
  if (yAxis && fieldNameChainMap?.yAxis[yAxis]) {
    conditionData[fieldNameChainMap.yAxis[yAxis]].push(...sourceData);
  }
  // 条件项存在
  if(!$isEmptyArr(yAxisConditionArr)) {
    yAxisConditionArr.forEach((yItem) => {
      const yConditionData = [];
      source.forEach((item) => {
          yConditionData.push({
            value: item[yItem],
          });
      });
      // 根据原始字段名称收集数据
      if(fieldNameChainMap['yAxisCondition'] && fieldNameChainMap['yAxisCondition'][yItem]) {
        conditionData[fieldNameChainMap['yAxisCondition'][yItem]].push(...yConditionData);
      }
    });
  }
  // 条件格式处理
  const options = {
    conditionData,
    isExistLegend: false, // 是否存在颜色维度
    seriesData,
    legendFieldName: null,
    fieldNameChainMap,
  };
  handleConditionExtend(options, conditionOptions);
  return {
    seriesData,
  };
}
